home *** CD-ROM | disk | FTP | other *** search
-
- /*
- Copyright 1992 - John K. Humkey
- */
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <conio.h>
- #include <string.h>
- #include <dos.h>
- #include "timespan.h"
- #include "timpoint.h"
-
- void astro_to_date(int *year, int *month, int *day, long *ast);
- void date_to_astro(int *year, int *month, int *day, long *ast);
- void time_to_astro(int *hour, int *mins, int *sec, int *millisec, long *ast);
- void astro_to_time(int *hour, int *mins, int *sec, int *millisec, long *ast);
-
- void span_demo();
- void point_demo();
-
- int __european;
-
- main()
- {
- __european = 0;
- span_demo();
- point_demo();
- }
-
- void span_demo()
- {
- timespan comboone(100L,0,29,29);
- timespan combotwo(100L,0,29,29);
- timespan combothree(200L,0,0,0);
- timespan combofour(100L,0,29,30);
- timespan combototal;
-
- clrscr();
- printf("\nTime Span Demo\n\n");
-
- printf("ts = ts + ts\n");
- combototal = comboone + combotwo;
- comboone.print();
- combotwo.print();
- combototal.print();
- printf("\n"); getch();
-
- printf("ts = ta - tb (a<b type 1)\n");
- combototal = comboone - combothree;
- comboone.print();
- combothree.print();
- combototal.print();
- printf("\n"); getch();
-
- printf("ts = ta - tb (a<b type 2)\n");
- combototal = comboone - combofour;
- comboone.print();
- combofour.print();
- combototal.print();
- printf("\n"); getch();
-
- printf("ts = ta - tb (a==b)\n");
- combototal = comboone - combotwo;
- comboone.print();
- combotwo.print();
- combototal.print();
- printf("\n"); getch();
-
- printf("ts = ta - tb (a>b type 1)\n");
- combototal = combothree - comboone;
- combothree.print();
- comboone.print();
- combototal.print();
- printf("\n"); getch();
-
- printf("ts = ta - tb (a>b type 2)\n");
- combototal = combofour - comboone;
- combofour.print();
- comboone.print();
- combototal.print();
- printf("\n"); getch();
-
- printf("ts = ts * 2\n");
- combototal = combothree * 2;
- combothree.print();
- combototal.print();
- printf("\n"); getch();
-
- printf("ts = ts / 2\n");
- combototal = combothree / 2;
- combothree.print();
- combototal.print();
- printf("\n"); getch();
-
- printf("ta < tb\n");
- printf("should be 1 is %d\n",comboone < combothree);
- printf("should be 1 is %d\n",comboone < combofour);
- printf("should be 0 is %d\n",comboone < combotwo);
- printf("should be 0 is %d\n",combothree < comboone);
- printf("should be 0 is %d\n",combofour < comboone);
- printf("\n"); getch();
-
- printf("ta > tb\n");
- printf("should be 0 is %d\n",comboone > combothree);
- printf("should be 0 is %d\n",comboone > combofour);
- printf("should be 0 is %d\n",comboone > combotwo);
- printf("should be 1 is %d\n",combothree > comboone);
- printf("should be 1 is %d\n",combofour > comboone);
- printf("\n"); getch();
-
- printf("ta == tb\n");
- printf("should be 0 is %d\n",comboone == combothree);
- printf("should be 0 is %d\n",comboone == combofour);
- printf("should be 1 is %d\n",comboone == combotwo);
- printf("should be 0 is %d\n",combothree == comboone);
- printf("should be 0 is %d\n",combofour == comboone);
- printf("\n"); getch();
-
- printf("ta != tb\n");
- printf("should be 1 is %d\n",comboone != combothree);
- printf("should be 1 is %d\n",comboone != combofour);
- printf("should be 0 is %d\n",comboone != combotwo);
- printf("should be 1 is %d\n",combothree != comboone);
- printf("should be 1 is %d\n",combofour != comboone);
- printf("\n"); getch();
-
- printf("ta <= tb\n");
- printf("should be 1 is %d\n",comboone <= combothree);
- printf("should be 1 is %d\n",comboone <= combofour);
- printf("should be 1 is %d\n",comboone <= combotwo);
- printf("should be 0 is %d\n",combothree <= comboone);
- printf("should be 0 is %d\n",combofour <= comboone);
- printf("\n"); getch();
-
- printf("ta >= tb\n");
- printf("should be 0 is %d\n",comboone >= combothree);
- printf("should be 0 is %d\n",comboone >= combofour);
- printf("should be 1 is %d\n",comboone >= combotwo);
- printf("should be 1 is %d\n",combothree >= comboone);
- printf("should be 1 is %d\n",combofour >= comboone);
- printf("\n"); getch();
-
- printf("ts += ts\n");
- combototal = comboone;
- combototal.print();
- combototal += combotwo;
- combotwo.print();
- combototal.print();
- printf("\n"); getch();
-
- printf("ts -= ts\n");
- combototal = combothree;
- combototal.print();
- combototal -= combotwo;
- combotwo.print();
- combototal.print();
- printf("\n"); getch();
- }
-
- void point_demo()
- {
- char str[80];
-
- timepoint comboone(1962,9,2,2,29,29);
- timespan combotwo(0,0,30,30);
- timepoint combothree(0,0,0,2,10,10);
- timepoint combototal;
-
- clrscr();
- printf("\nTime Point Demo\n\n");
-
- combototal = comboone + combotwo;
- comboone.print();
- combotwo.print();
- combototal.print();
-
- printf("\nTime Point String Demo\n\n");
- printf("American format\n");
- printf("mdy2w %s\n",comboone.mdy2w(str));
- printf("mdy4w %s\n",comboone.mdy4w(str));
- printf("mdy2 %s\n",comboone.mdy2(str));
- printf("mdy4 %s\n",comboone.mdy4(str));
- printf("y2md %s\n",comboone.y2md(str));
- printf("y4md %s\n",comboone.y4md(str));
-
- printf("\nEuropean format\n");
- __european = 1;
- printf("mdy2w %s\n",comboone.mdy2w(str));
- printf("mdy4w %s\n",comboone.mdy4w(str));
- printf("mdy2 %s\n",comboone.mdy2(str));
- printf("mdy4 %s\n",comboone.mdy4(str));
- printf("y2md %s\n",comboone.y2md(str));
- printf("y4md %s\n",comboone.y4md(str));
-
- getch();
- }
-
- /* # # #### ##### # #### ###### */
- /* ## # # # # # # # # */
- /* # # # # # # # # ##### */
- /* # # # # # # # # # */
- /* # ## # # # # # # # */
- /* # # #### # # #### ###### */
- /* */
-
- /* The following routines are used by the timepoint */
- /* and timespan classes, and will need to be placed */
- /* in your normal user libraries (or included in */
- /* source form in your code.) */
-
- /* The following routines... */
- /* */
- /* astro_to_date */
- /* date_to_astro */
- /* astro_to_time */
- /* time_to_astro */
- /* */
- /* were taken from an article in "68' Micro Journal"*/
- /* from Volume VIII Issue II * February 1986. */
- /* */
- /* To the best of my knowledge the magazine is out */
- /* of business, which is a shame because they pub- */
- /* lished some interesting source code. */
- /* */
- /* The article was under "C" User Notes and was by */
- /* Edgar M. (Bud) Pass, Ph.D. */
- /* Mr. Pass adapted the routines from ... */
- /* "Communications of the ACM * Oct 68 p657, Oct 70 */
- /* p621, Oct 72 p918. */
- /* */
- /* Several other routines are included in the */
- /* original article (to calculate the day of the */
- /* year, day of the week) but are not needed for the*/
- /* timepoint and timespan classes. */
- /* */
- /* I don't pretend to understand the underlying */
- /* theory of how the routines work, I just know that*/
- /* used in combination with the timepoint and */
- /* timespan classes they can accurately represent */
- /* any point from about 4000 years ago to any point */
- /* in the future (with down to the millisecond */
- /* resolution). */
-
- void astro_to_date(int *year, int *month, int *day, long *ast)
- {
- long t1,t2,t3,t4,t5;
-
- if (*ast==0L) {
- *year = 0; *month = 0; *day = 0;
- return;
- }
-
- t4 = *ast + 68569;
- t5 = (4 * t4) / 146097;
- t1 = (146097 * t5 + 3) / 4;
- t4 = t4 - t1;
- t1 = (4000 * (t4 + 1)) / 1461001;
- t2 = (1461 * t1) / 4;
- t4 = t4 - t2 + 31;
- t2 = (80 * t4) / 2447;
- t3 = (2447 * t2) / 80;
- t3 = t4 - t3;
- t4 = t2 / 11;
- t2 = t2 + 2 - 12 * t4;
- t1 = 100 * (t5 - 49) + t1 + t4;
- *year = t1; *month = t2; *day = (t3 & 31);
- }
-
- void date_to_astro(int *year, int *month, int *day, long *ast)
- {
- long t1,t2,t3;
-
- if ((*year==0)&&(*month==0)&&(*day==0)) {
- *ast = 0L;
- return;
- }
-
- if (*month == 0) *month = 1;
- if (*day == 0) *day = 1;
-
- t1 = (*month - 14) / 12;
- t2 = (1461 * (*year + 4800 + t1)) / 4;
- t3 = (367 * (*month - 2 - t1 * 12)) / 12;
- t1 = (3 * ((*year + 4900 + t1) / 100)) / 4;
- *ast = (long)(*day - 32075 + t2 + t3 - t1);
- }
-
- void time_to_astro(int *hour, int *mins, int *sec, int *millisec, long *ast)
- {
- long t1,t2,t3;
-
- t1 = (long)(*hour) * 3600000L;
- t2 = (long)(*mins) * 60000L;
- t3 = (long)(*sec) * 1000L;
- *ast = t1 + t2 + t3 + *millisec;
- }
-
- void astro_to_time(int *hour, int *mins, int *sec, int *millisec, long *ast)
- {
- long t1,t2,t3,t4,t5;
-
- t1 = *ast;
- *millisec = (int)(t1 % 1000L);
- t1 /= 1000L;
- *sec = (int)(t1 % 60L);
- t1 /= 60L;
- *mins = (int)(t1 % 60L);
- t1 /= 60L;
- *hour = (int)(t1 % 60L);
- }
-
-